/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import entities.Cajas;
import entities.MovimientosCajaCabecera;

import entities.MovimientosCajaDetalle;
import entities.MovimientosCajaDetallePK;
import entities.TiposOperaciones;
import entities.Usuarios;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

/**
 *
 * @author edgar
 */
@ManagedBean(name = "gestionarCajas")
@ViewScoped
public class GestionarCajasController {

    private Cajas caja;
    private Usuarios cajero = new Usuarios(1);
    private Integer saldoInicial;
    @PersistenceContext(unitName = "sgal3PU")
    EntityManager em;

    public String habilitarCaja() {

        if (!caja.getAbierta()) {
            UserTransaction t;
            try {
                t = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
                t.begin();
                MovimientosCajaCabecera mcc = new MovimientosCajaCabecera();
                mcc.setNroCaja(caja);
                mcc.setCodUsuario(cajero);
                mcc.setFechaHoraApertura(new Date());
                mcc.setSaldoInicial(getSaldoInicial());
                em.persist(mcc);
                MovimientosCajaDetalle mcd = new MovimientosCajaDetalle();
                MovimientosCajaDetallePK mcdpk = new MovimientosCajaDetallePK();
                mcdpk.setNroMovimiento(mcc.getNroMovimiento());
                mcdpk.setNroLinea(0);
                mcd.setMovimientosCajaDetallePK(mcdpk);
                mcd.setCodTipoOperacion(new TiposOperaciones(1));
                mcd.setMonto(getSaldoInicial());
                mcd.setNroCaja(caja);
                em.persist(mcd);
                caja.setAbierta(true);
                caja.setCajeroActual(cajero);
                caja.setSaldoActual(saldoInicial);
                em.merge(caja);
                t.commit();
                this.reset_campos();
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Caja habilitada correctamente"));

            } catch (NamingException ex) {
                Logger.getLogger(GestionarCajasController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NotSupportedException ex) {
                Logger.getLogger(GestionarCajasController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SystemException ex) {
                Logger.getLogger(GestionarCajasController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RollbackException ex) {
                Logger.getLogger(GestionarCajasController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (HeuristicMixedException ex) {
                Logger.getLogger(GestionarCajasController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (HeuristicRollbackException ex) {
                Logger.getLogger(GestionarCajasController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(GestionarCajasController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalStateException ex) {
                Logger.getLogger(GestionarCajasController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "La caja ya se encuentra abierta"));
        }
        return null;
    }

    public void reset_campos(){
        this.saldoInicial=null;
     }
    /**
     * @return the caja
     */
    public Cajas getCaja() {
        return caja;
    }

    /**
     * @param caja the caja to set
     */
    public void setCaja(Cajas caja) {
        this.caja = caja;
    }

    /**
     * @return the cajero
     */
    public Usuarios getCajero() {
        return cajero;
    }

    /**
     * @param cajero the cajero to set
     */
    public void setCajero(Usuarios cajero) {
        this.cajero = cajero;
    }

    /**
     * @return the saldoInicial
     */
    public Integer getSaldoInicial() {
        return saldoInicial;
    }

    /**
     * @param saldoInicial the saldoInicial to set
     */
    public void setSaldoInicial(Integer saldoInicial) {
        this.saldoInicial = saldoInicial;
    }
}
